package org.infoset.xml.memory;

import java.io.*;
import java.util.*;
import java.net.*;

import org.infoset.xml.*;


public class MemoryAttribute extends MemoryTextDataItem implements Attribute {

   protected int line;
   protected int column;
   protected List<String> tokens;
   protected Name name;
   protected String prefix;
   protected int validity;
   protected int validationAttempted;
   protected List errors;
   
   public MemoryAttribute(Infoset infoset,Parent origin,Name name,String textValue,Object typedValue) {
      super(infoset,origin);
      this.line = 0;
      this.column = 0;
      this.name = name;
      this.prefix = Name.NO_PREFIX;
      setText(textValue);
      setTypedValue(typedValue);
      this.tokens = null;
      this.validity = NOT_KNOWN;
      this.validationAttempted = ATTEMPTED_NONE;
      this.typeDefinition = MemoryElement.anySimpleType;
      this.errors = null;
   }

   protected void setIndex(int index) {
   }

   public ItemType getType() {
      return ItemType.AttributeItem;
   }

   public Name getName() {
      return name;
   }

   public void setName(Name value) {
      this.name = value;
   }

   public void setName(String localName) {
      this.name = infoset.createName(localName);
   }

   public void setName(URI namespaceName,String localName) {
      this.name = infoset.createName(namespaceName,localName);
   }
   
   public String getPrefix() {
      return prefix;
   }
   
   public void setPrefix(String prefix) {
      if (prefix==null) {
         throw new IllegalArgumentException("Prefix cannot be a null value.");
      }
      this.prefix = prefix;
   }
   
   public List<String> getTokenizedValue() {
      if (!(data instanceof String)) {
         return null;
      }

      if (tokens!=null) {
         return tokens;
      }

      String value = ((String)data).trim();
      
      tokens = new ArrayList();
      do {
         int space = value.indexOf(' ');
         if (space>0) {
            tokens.add(value.substring(0,space));
            value = value.substring(space).trim();
         } 
      } while(value.length()!=0);
      return tokens;
   }
   
   public void setText(String textValue) {
      super.setText(textValue);
      tokens = null;
   }
   
   public Element getElement() {
      return (Element)getOrigin();
   }

   public int getColumn() {
      return column;
   }   

   public int getLine() {
      return line;
   }
   
   public void setColumn(int column) {
      this.column = column;
   }
   
   public void setLine(int line) {
      this.line = line;
   }
   
   public int getValidity() {
      return validity;
   }
   
   public int getValidationAttempted() {
      return validationAttempted;
   }
   
   public void setValidity(int validity, int attempted) {
      this.validity = validity;
      this.validationAttempted = attempted;
   }
   
   
   public Iterator getErrors() {
      return errors==null ? Collections.EMPTY_LIST.iterator() : errors.iterator();
   }

   public void setErrors(List errorStringList)
   {
      errors = new ArrayList();
      errors.addAll(errorStringList);
   }
   
   public void addError(String error) {
      if (errors==null) {
         errors = new ArrayList();
      }
      errors.add(error);
   }
   public Item copyOfItem(boolean copyAll) {
      return new MemoryAttribute(infoset,null,name,value,data);
   }
   
}
